package com.jy.novel.dao;

import com.jy.novel.common.JdbcHelper;
import com.jy.novel.common.Page;
import com.jy.novel.pojo.NovelCategory;
import com.jy.novel.pojo.form.NovelCategoryAddForm;
import com.jy.novel.pojo.form.NovelCategoryEditForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jihb on 4/28/16.
 */
@Repository
public class NovelCategoryDao {

    @Autowired
    JdbcHelper jdbcHelper;

    /**
     * 查询分类列表 分页
     * @param parentId
     * @param curPage
     * @param pageSize
     * @return
     */
    public Page<NovelCategory> findNovelCategoryPage(long parentId,int curPage,int pageSize){
        String sql = "select id,parentId,name,leaf,level from t_novel_category where parentId=? ";
        return jdbcHelper.queryForPageBean(sql,new Object[]{parentId},NovelCategory.class,curPage,pageSize);
    }

    /**
     * 查询分类列表 分页
     * @param parentId
     * @param curPage
     * @param pageSize
     * @return
     */
    public List<NovelCategory> findNovelCategoryList(long parentId,int curPage,int pageSize){
        String sql = "select id,parentId,name,leaf,level from t_novel_category where parentId=? ";
        return jdbcHelper.queryForListBean(sql,new Object[]{parentId},NovelCategory.class,curPage,pageSize);
    }

    /**
     * 查询分类列表 不分页
     * @param parentId
     * @return
     */
    public List<NovelCategory> findNovelCategoryList(long parentId){
        String sql = "select id,parentId,name,leaf,level from t_novel_category where parentId=? ";
        return jdbcHelper.queryForListBean(sql,new Object[]{parentId},NovelCategory.class);
    }

    /**
     * 添加分类
     * @param addForm
     * @return
     */
    public boolean addNovelCategory(NovelCategoryAddForm addForm){
        String sql = "insert into t_novel_category(parentId,name,leaf,level) values(?,?,?,?)";
        return jdbcHelper.update(sql,new Object[]{addForm.getParentId(),addForm.getName(),addForm.getLeaf(),addForm.getLevel()})>0;
    }

    /**
     * 修改分类
     * @param editForm
     * @param id
     * @return
     */
    public boolean updateNovelCategory(NovelCategoryEditForm editForm,long id){
        StringBuilder sb = new StringBuilder();
        sb.append("update t_novel_category set id=id");
        List params = new ArrayList();
        if (editForm.getParentId() != null){
            sb.append(",parentId=?");
            params.add(editForm.getParentId());
        }
        if (editForm.getName() != null){
            sb.append(",name=?");
            params.add(editForm.getName());
        }
        if (editForm.getLeaf() != null){
            sb.append(",leaf=?");
            params.add(editForm.getLeaf());
        }
        if (editForm.getLevel() != null){
            sb.append(",levle=?");
            params.add(editForm.getLevel());
        }
        sb.append(" where id=? ");
        params.add(id);
        return jdbcHelper.update(sb.toString(),params.toArray())>0;
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    public boolean deleteNovelCategory(long id){
        String sql = "delete from t_novel_category where id=? ";
        return jdbcHelper.update(sql,new Object[]{id})>0;
    }
}
